import { batchConfig } from "../api/Interface/app";
import { subscribeEnum } from "../enums/subscribe";
import errorCode from "../utils/http/errorCode";
import { useGetConfig } from "./useCommon";

/**
 * 订阅模版消息
 */
export const useSubscribeTemplate = (tmplIds = []) => {
  if (!tmplIds || tmplIds.length === 0) return;
  return new Promise((resolve, reject) => {
    batchConfig({ key: tmplIds.join(",") }).then((res) => {
      if (res.code === errorCode.success) {
        const { data = {} } = res;
        const ids = Object.values(data).filter((m) => m);
        uni.requestSubscribeMessage({
          tmplIds: ids,
          success: (success) => {
            console.log("消息订阅", success);
            resolve({
              code: errorCode.success,
              data: success,
            });
          },
          fail: (fail) => {
            console.log("消息订阅fail", fail);
            reject({
              code: errorCode.default,
              data: fail,
            });
          },
          complete: (complete) => {},
        });
      } else {
        resolve({
          code: errorCode.default,
          data: null,
        });
      }
    });
  });
};

/**
 * 订阅模版消息
 */
export const _useSubscribeTemplate = (tmplIds = []) => {
  if (!tmplIds || tmplIds.length === 0) return;
  return new Promise((resolve, reject) => {
    uni.requestSubscribeMessage({
      tmplIds,
      success: (success) => {
        console.log("消息订阅", success);
        resolve({
          code: errorCode.success,
          data: success,
        });
      },
      fail: (fail) => {
        console.log("消息订阅fail", fail);
        reject({
          code: errorCode.default,
          data: fail,
        });
      },
      complete: (complete) => {},
    });
  });
};
